package com.bldby.baselibrary.core.addresspick;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bldby.baselibrary.R;
import com.bldby.baselibrary.app.GlobalUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class AddressPickerUtil {
    //省
    private List<Regions> regions;
    //市
    private List<List<CityListBean>> provinceCityLists1 = new ArrayList<>();
    //县
    private List<List<List<AreaListBean>>> provinceCityLists2 = new ArrayList<>();

    public AddressPickerUtil(List<Regions> regions) {
        if (regions == null) {
            regions = new ArrayList<>();
        }
        this.regions = regions;
        for (int i = 0; i < regions.size(); i++) {
            Regions regions1 = this.regions.get(i);
            List<CityListBean> cityList = regions1.getCityList();
            if (cityList == null) {
                cityList = new ArrayList<>();
            }
            provinceCityLists1.add(cityList);
            List<List<AreaListBean>> provinc = new ArrayList<>();
            for (int j = 0; j < cityList.size(); j++) {
                List<AreaListBean> areaList = cityList.get(j).getAreaList();
                if (areaList == null) {
                    areaList = new ArrayList<>();
                }
                provinc.add(areaList);
            }
            provinceCityLists2.add(provinc);
        }
    }

    OptionsPickerView pvOptions;
    public OptionsPickerView getOptionsPickerView(Activity context, boolean sanji, OnOptionsTextSelectListener onOptionsSelectListener) {
//        System.exit(0);
        pvOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                Regions region = regions.get(options1);
                CityListBean cityListBean = new CityListBean();
                AreaListBean areaListBean = new AreaListBean();
                if (sanji) {
                    //省市区
                    if (provinceCityLists1.get(options1) != null && provinceCityLists1.get(options1).size() > 0) {
                        cityListBean = provinceCityLists1.get(options1).get(options2);
                    }

                    if (provinceCityLists1.get(options1) != null && provinceCityLists1.get(options1).size() > 0
                            && provinceCityLists2.get(options1).get(options2) != null && provinceCityLists2.get(options1).get(options2).size() > 0) {
                        areaListBean = provinceCityLists2.get(options1).get(options2).get(options3);
                    }

                } else {
                    // 省 市
                    if (provinceCityLists1.get(options1) != null && provinceCityLists1.get(options1).size() > 0) {
                        cityListBean = provinceCityLists1.get(options1).get(options2);
                    }
                }

                onOptionsSelectListener.onOptionsSelect(region, cityListBean, areaListBean);
            }
        })
//                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
//                    @Override
//                    public void customLayout(View v) {
//                        ImageView viewById = v.findViewById(R.id.back);
//                        viewById.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                pvOptions.returnData();
//                                pvOptions.dismiss();
//                            }
//                        });
//                    }
//                })
                .setSubmitText(context.getString(R.string.confirm))//确定按钮文字
                .setCancelText(context.getString(R.string.cancel))//取消按钮文字
                .setTitleText(context.getString(R.string.chooseAre))//标题
                .setSubCalSize(18)//确定和取消文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLACK)//确定按钮文字颜色
                .setCancelColor(Color.BLACK)//取消按钮文字颜色
                .setTextColorCenter(Color.BLACK)
                .setTextColorOut(Color.BLACK)
                .setTitleBgColor(GlobalUtil.getCurrentActivity().getResources().getColor(R.color.white))//标题背景颜色 Night mode
                .setBgColor(GlobalUtil.getCurrentActivity().getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                .setContentTextSize(18)//滚轮文字大小
//                .setLabels("省", "市", "区")//设置选择的三级单位
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(1, 1, 1)  //设置默认选中项
                .setOutSideCancelable(true)//点击外部dismiss default true
                .setDecorView(context.getWindow().getDecorView().findViewById(android.R.id.content))
                .isDialog(false)//是否显示为对话框样式
                .build();


        if (sanji) {
            //省市区
            pvOptions.setPicker(regions, provinceCityLists1, provinceCityLists2);//添加数据源
            pvOptions.setSelectOptions(0, 0, 0);

        } else {
            // 省 市
            pvOptions.setPicker(regions, provinceCityLists1);//添加数据源
            pvOptions.setSelectOptions(0, 0);

        }

        return pvOptions;
    }

    private String readString(InputStream in) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            StringWriter sw = new StringWriter();
            String line;
            while ((line = br.readLine()) != null) {
                sw.write(line);
            }
            br.close();
            sw.close();
            return sw.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }

    public interface OnOptionsTextSelectListener {

        void onOptionsSelect(Regions options1, CityListBean options2, AreaListBean options3);

    }
}
